package com.bachelor.community.service.impl;

import com.bachelor.community.mapper.QuestionTagMapper;
import com.bachelor.community.model.entity.QuestionTagRelation;
import com.bachelor.community.model.entity.Tag;
import com.bachelor.community.service.intf.QuestionTagService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class QuestionTagServiceImpl extends ServiceImpl<QuestionTagMapper, QuestionTagRelation> implements QuestionTagService {

    public final QuestionTagMapper questionTagMapper;

    @Autowired
    public QuestionTagServiceImpl(QuestionTagMapper questionTagMapper) {
        this.questionTagMapper = questionTagMapper;
    }

    @Override
    public List<QuestionTagRelation> selectByQuestionId(Long questionId) {
        QueryWrapper<QuestionTagRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("question_id", questionId);
        return questionTagMapper.selectList(wrapper);
    }

    @Override
    public void createQuestionTag(Long id, List<Tag> tags) {
        QueryWrapper<QuestionTagRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("question_id", id);
        questionTagMapper.delete(queryWrapper);
        tags.forEach(tag -> {
            QuestionTagRelation questionTag = new QuestionTagRelation();
            questionTag.setQuestionId(id);
            questionTag.setTagId(tag.getId());
            save(questionTag);
        });
    }

    @Override
    public Set<Long> listQuestionIdsByTagId(Integer id) {
        return questionTagMapper.selectQuestionIdsByTagId(id);
    }
}
